package com.bytedance.article.a.a;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;

import com.bytedance.common.utility.collection.WeakList;
import com.bytedance.frameworks.base.mvp.Presenter;
import com.ss.android.common.app.ActivityStackManager;
import com.ss.android.common.app.AppHooks;
import com.ss.android.common.app.LifeCycleMonitor;
import com.ss.android.common.app.IComponent;
import com.ss.android.common.app.LifeCycleInvoker;
import com.ss.android.common.app.permission.PermissionsManager;
import com.ss.android.common.app.permission.RequestPermissionsRequestCodeValidator;
import com.ss.android.common.app.permission.OnRequestPermissionsResultCallback;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.ImmersedStatusBarHelper;
import com.ss.android.newmedia.a.OverridePendingTransitionHelper;
import com.ss.android.newmedia.a.InputMethodManagerUtil;
import java.util.Iterator;

public abstract class a<P extends Presenter> extends com.bytedance.frameworks.a.a.b<P> implements
        ActivityStackManager.StackRecorder,
        IComponent,
        LifeCycleInvoker,
        RequestPermissionsRequestCodeValidator,
        OnRequestPermissionsResultCallback {
    public boolean mDelayOverrideActivityTrans;
    public ImmersedStatusBarHelper mImmersedStatusBarHelper;
    private boolean isActive;
    private boolean isDestroyed;
    private WeakList f;
    private BroadcastReceiver mBroadcastReceiver;
    private String mRecorderKey;

    public a() {
        this.mDelayOverrideActivityTrans = false;
        this.isActive = false;
        this.isDestroyed = false;
        this.f = new WeakList();
    }

    public void finish() {
        super.finish();
        if(!this.mDelayOverrideActivityTrans) {
            OverridePendingTransitionHelper.overrideExitPendingTransition(this, 0);
        }
    }

    public String getRecorderKey() {
        return this.mRecorderKey;
    }

    public boolean isActive() {
        return this.isActive;
    }

    public boolean isDestroyed() {
        boolean v0;
        if(Build.VERSION.SDK_INT < 17) {
            v0 = this.isDestroyed;
            return v0;
        }

        try {
            v0 = super.isDestroyed();
        }
        catch(Throwable v0_1) {
            v0 = this.isDestroyed;
        }

        return v0;
    }

    public boolean isViewValid() {
        return !this.isDestroyed;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        AppHooks.ActivityResultHook activityResultHook = AppHooks.getActivityResultHook();
        if(activityResultHook == null || !activityResultHook.onActivityResult(this, requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        long v4 = 0;
        this.mImmersedStatusBarHelper = new ImmersedStatusBarHelper(this, new ImmersedStatusBarHelper.ImmersedStatusBarConfig());
        this.mImmersedStatusBarHelper.setup();
        super.onCreate(savedInstanceState);
        if(com.bytedance.article.common.a.h.q() > v4) {
            if(System.currentTimeMillis() - com.bytedance.article.common.a.h.q() > 3000) {
                com.bytedance.article.common.a.h.d(v4);
            }
            com.bytedance.article.common.a.h.e(v4);
        }

        this.mRecorderKey = savedInstanceState == null
                || !savedInstanceState.containsKey("abs_Activity_Key")
                ? ActivityStackManager.buildKey(this)
                : savedInstanceState.getString("abs_Activity_Key");
        AppHooks.InitHook initHook = AppHooks.getInitHook();
        if(initHook != null) {
            initHook.tryInit(this);
        }
        this.mBroadcastReceiver = new ExitAppBroadcastReceiver(this);
        LocalBroadcastManager.getInstance(this).registerReceiver(this.mBroadcastReceiver, new IntentFilter("com.ss.android.common.app.action.exit_app"));
        ActivityStackManager.addRecorder(this);
        this.mDelayOverrideActivityTrans = this.getIntent().getBooleanExtra("delay_override_activity_trans", false);
        if(!this.mDelayOverrideActivityTrans) {
            OverridePendingTransitionHelper.overrideEnterPendingTransition(this, 0);
        }

        ++AppHooks.mForegroundActivityNum;
    }

    public void onDestroy() {
        LocalBroadcastManager.getInstance(((Context)this)).unregisterReceiver(this.mBroadcastReceiver);
        super.onDestroy();
        InputMethodManagerUtil.fixMemoryLeak(((Activity)this));
        this.isDestroyed = true;
        if(!this.f.weakHashMap.isEmpty()) {
            Iterator v1 = this.f.iterator();
            while(v1.hasNext()) {
                Object v0 = v1.next();
                if(v0 == null) {
                    continue;
                }

                ((LifeCycleMonitor)v0).onDestroy();
            }

            this.f.weakHashMap.clear();
        }

        ActivityStackManager.removeRecorder(this);
        --AppHooks.mForegroundActivityNum;
    }

    public void onPause() {
        super.onPause();
        this.isActive = false;
        MobClickCombiner.onPause(this);
        AppHooks.ActivityLifeCycleHook activityHook = AppHooks.getActivityHook();
        if(activityHook != null) {
            activityHook.onActivityPaused(((Activity)this));
        }

        if(!this.f.weakHashMap.isEmpty()) {
            Iterator v1 = this.f.iterator();
            while(v1.hasNext()) {
                Object v0_1 = v1.next();
                if(v0_1 == null) {
                    continue;
                }
                ((LifeCycleMonitor)v0_1).onPause();
            }
        }
    }

    public void onRequestPermissionsResult(int arg3, @NonNull String[] arg4, @NonNull int[] arg5) {
        super.onRequestPermissionsResult(arg3, arg4, arg5);
        if((arg3 >> 8 & 255) == 0) {
            PermissionsManager.getInstance().notifyPermissionsChange(this, arg4, arg5, true);
        }
    }

    protected void onRestoreInstanceState(Bundle arg2) {
        super.onRestoreInstanceState(arg2);
        if(arg2 != null && (arg2.containsKey("abs_Activity_Key"))) {
            this.mRecorderKey = arg2.getString("abs_Activity_Key");
        }
    }

    public void onResume() {
        super.onResume();
        this.isActive = true;
        MobClickCombiner.onResume(this);
        AppHooks.AppStartMonitorHook appStartMonitorHook = AppHooks.getAppStartMonitorHook();
        if(appStartMonitorHook != null && com.bytedance.article.common.a.h.l() > 0) {
            appStartMonitorHook.onAppStartMonitor();
        }

        AppHooks.ActivityLifeCycleHook activityHook = AppHooks.getActivityHook();
        if(activityHook != null) {
            activityHook.onActivityResumed(this);
        }

        if(!this.f.weakHashMap.isEmpty()) {
            Iterator v1 = this.f.iterator();
            while(v1.hasNext()) {
                Object v0_2 = v1.next();
                if(v0_2 == null) {
                    continue;
                }

                ((LifeCycleMonitor)v0_2).onResume();
            }
        }
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        if(bundle != null) {
            bundle.putString("abs_Activity_Key", this.mRecorderKey);
            bundle.putBoolean("WORKAROUND_FOR_BUG_19917_KEY", true);
        }
    }

    public void onStart() {
        super.onStart();
        if(AppHooks.mAvailableActivityNum == 0) {
            AppHooks.AppBackgroundHook v0 = AppHooks.getAppBackgroundHook();
            if(v0 != null) {
                v0.onAppBackgroundSwitch(false);
            }
        }

        ++AppHooks.mAvailableActivityNum;
    }

    public void onStop() {
        super.onStop();
        int v0 = AppHooks.mAvailableActivityNum - 1;
        AppHooks.mAvailableActivityNum = v0;
        if(v0 == 0) {
            AppHooks.AppBackgroundHook v0_1 = AppHooks.getAppBackgroundHook();
            if(v0_1 != null) {
                v0_1.onAppBackgroundSwitch(true);
            }
        }

        this.isActive = false;
        if(!this.f.weakHashMap.isEmpty()) {
            Iterator v1 = this.f.iterator();
            while(v1.hasNext()) {
                Object v0_2 = v1.next();
                if(v0_2 == null) {
                    continue;
                }

                ((LifeCycleMonitor)v0_2).onStop();
            }
        }
    }

    public void registerLifeCycleMonitor(LifeCycleMonitor arg2) {
        this.f.add(arg2);
    }

    public void unregisterLifeCycleMonitor(LifeCycleMonitor arg2) {
        this.f.remove(arg2);
    }
}

